//
//  FPBankCardModel.m
//  FinanceProject
//
//  Created by 陈显摆 on 2025/1/23.
//

#import "FPBankCardModel.h"







@implementation BankCardErrorMessage

+ (JSONKeyMapper *)keyMapper {
    
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"ID" : @"id"}];
}

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
    
    return YES;
}

@end





@implementation FPBankCardModel

+ (JSONKeyMapper *)keyMapper {
    
    return [[JSONKeyMapper alloc] initWithModelToJSONDictionary:@{@"ID" : @"id"}];
}

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
    
    return YES;
}

+ (void)saveBankCardWithParam:(NSDictionary *)param success:(void (^)(NSInteger, NSString * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:ADD_BANK_CARD_URL] parameters:param type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)getCardListSuccess:(void (^)(NSInteger, NSString * _Nonnull, NSArray * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    
    [param setObject:@(1) forKey:@"pageNum"];
    [param setObject:@(20) forKey:@"pageSize"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:GET_CARD_LIST_URL] parameters:param type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSArray *list = [FPBankCardModel arrayOfModelsFromDictionaries:[object objectForKey:@"data"] error:&error];
        
        for (FPBankCardModel *model in list) {
            
            model.phone = [FPUserModel AES256Decrypt:model.phone];
            model.bankCard = [FPUserModel AES256Decrypt:model.bankCard];
            
            model.isSelect = NO;
        }
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode, message, list);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)getBankInfoWithCardNumber:(NSString *)cardNo success:(void (^)(FPBankCardModel * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSString *URLStr = [NSString stringWithFormat:@"https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?cardNo=%@&cardBinCheck=true", cardNo];
    
    [[APIClient sharedInstance] requestWithURLString:URLStr parameters:@{} type:HttpRequestTypeGet success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        FPBankCardModel *model = [[FPBankCardModel alloc] initWithDictionary:object error:&error];
        
        if ([NSString isBlankString:model.bank]) {
            
            model.bank = @"请检查卡号或补充完整";
        }
        else {
            
            NSString *type;
            
            if ([model.cardType isEqualToString:@"DC"]) {
                
                type = @"储蓄卡";
            }
            else if ([model.cardType isEqualToString:@"CC"]) {
                
                type = @"信用卡";
            }
            else if ([model.cardType isEqualToString:@"SCC"]) {
                
                type = @"准贷记卡";
            }
            else if ([model.cardType isEqualToString:@"PC"]) {
                
                type = @"预付费卡";
            }
            
            model.cardType = type;
            
            NSDictionary *bankNames = [FPBankCardModel readJSONFileWithName:@"BankCardLocationInfo"];
            
            if ([bankNames isKindOfClass:NSDictionary.class]) {
             
                model.bank = [bankNames objectForKey:model.bank];
            }
            
//            NSDictionary *bankLogos = [FPBankCardModel readJSONFileWithName:@"BankCardLogoInfo"];
//            
//            if ([bankLogos isKindOfClass:NSDictionary.class]) {
//             
//                model.logo = [bankLogos objectForKey:model.bank];
//            }
        }
        
        if (success) {
            
            success(model);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}

+ (NSDictionary *)readJSONFileWithName:(NSString *)fileName {
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"json"];
    NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
    
    NSError *error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                             options:NSJSONReadingMutableContainers
                                                               error:&error];
    
    if (error) {
        
        NSLog(@"JSON 解析错误: %@", error.localizedDescription);
        return nil;
        
    } else {
        
        return jsonDict;
    }
    
    return nil;
}


+ (void)deleteBankCardWithID:(NSString *)cardID success:(void (^)(NSInteger, NSString * _Nonnull))success failure:(void (^)(NSError * _Nonnull))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    [param setObject:cardID forKey:@"id"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:DELETE_BANK_CARD_URL] parameters:param type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}


#warning url 和 json解析没做
+ (void)getOrderStatusWithID:(NSString *)ID success:(void (^)(NSInteger code , NSString *message))success failure:(void (^)(NSError *error))failure {
    
    NSMutableDictionary *param = APIClient.creatAPIDictionary;
    
    [param setObject:ID forKey:@"ID"];
    
    [[APIClient sharedInstance] requestWithURLString:[MAIN_URL stringByAppendingString:@"url~~~"] parameters:@{} type:HttpRequestTypePost success:^(id responseObject, NSString *msg, NSInteger code) {
        
        NSError *error;
        
        id object = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:&error];
        
        NSInteger httpCode = [[object objectForKey:@"code"] integerValue];
        
        NSString *message = [object objectForKey:@"msg"];
        
        if (success) {
            
            success(httpCode , message);
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
    }];
}


@end
